package com.dbsop;

import com.dbsop.config.EsClientConfig;
import com.dbsop.mapper.ESBaseMapper;
import com.dbsop.request.ClientOpt;
import com.dbsop.rest.RestOpt;
import com.dbsop.transport.TcOpt;
import lombok.extern.slf4j.Slf4j;
import java.util.List;

@Slf4j
public class EsClient {

    private static EsClient esClient = null;

    private ClientOpt clientOpt;

    private boolean useHttp =false;

    private EsClientConfig esClientConfig;

    public static EsClient getInstance(EsClientConfig esClientConfig, List<ESBaseMapper> esBaseMappers){
        if (esClient == null){
            esClient = new EsClient(esClientConfig, esBaseMappers);
        }
        return esClient;
    }

    public static EsClient getEsClient() {
        return esClient;
    }

    private EsClient(EsClientConfig esClientConfig, List<ESBaseMapper> esBaseMappers) {
        this.esClient = this;
        this.esClientConfig = esClientConfig;
        initUseType();
        if (useHttp){
            clientOpt = new RestOpt(esClientConfig,esBaseMappers);
        }else {
            clientOpt = new TcOpt(esClientConfig,esBaseMappers);
        }
        clientOpt.init();
    }

    private void initUseType(){
           if ("http".equals(esClientConfig.getUseType())){
               useHttp = true;
           }
    }

    public ClientOpt getClientOpt() {
        return clientOpt;
    }

    public boolean isUseHttp() {
        return useHttp;
    }
}
